package com.forever7776.life.core.service.impl.send;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.forever7776.life.core.common.util.ResultVoUtil;
import com.forever7776.life.core.entity.send.SendEmailTemplate;
import com.forever7776.life.core.mapper.send.SendEmailTemplateMapper;
import com.forever7776.life.core.service.send.ISendEmailTemplateService;
import com.forever7776.life.remote.common.vo.ResultVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 邮件模板表 服务实现类
 * </p>
 *
 * @author kz
 * @since 2018-03-08
 */
@Service
public class SendEmailTemplateServiceImpl extends ServiceImpl<SendEmailTemplateMapper, SendEmailTemplate> implements ISendEmailTemplateService {


    /**
     * 根据邮件模板类型查询一条数据
     *
     * @param type
     * @return
     */
    @Override
    public SendEmailTemplate selectByType(String type) {
        return selectOne(new EntityWrapper<SendEmailTemplate>().eq("type", type).eq("status", 0));
    }

    /**
     * 查询邮件模板列表
     *
     * @param p
     * @param title
     * @param status
     * @return
     */
    @Override
    public Page<SendEmailTemplate> selectAll(Page<SendEmailTemplate> p, String title, Integer status) {
        EntityWrapper<SendEmailTemplate> ew = new EntityWrapper<SendEmailTemplate>();
        if (StringUtils.isNotEmpty(title)) {
            ew.like("title", title);
        }
        if (status != null) {
            ew.eq("status", status);
        }
        return selectPage(p, ew);
    }

    /**
     * 邮件模板是否生效
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public ResultVO frozen(String id, Integer status) {
        SendEmailTemplate t = selectById(id);
        if (t == null) {
            return ResultVoUtil.getFailResult("该邮件模板不存在！");
        }
        t.setStatus(status);
        this.updateById(t);
        return ResultVoUtil.getSuccessResult("操作成功！");
    }
}
